package com.beetobe.main;

import java.io.IOException;

import android.os.AsyncTask;

import com.beetobe.callib.CalendarLibHelper;
import com.beetobe.callib.event.AsyncInsertEvent;
import com.google.api.services.calendar.model.AclRule;
import com.google.api.services.calendar.model.AclRule.Scope;

public class AsyncAddFriend extends AsyncTask<Void, Void, Void> {
	MainActivity mainActivity;
	private int FROM_ADDFRIEND = 3;
	public AsyncAddFriend(MainActivity mainActivity) {
		this.mainActivity = mainActivity;

	}

	@Override
	protected Void doInBackground(Void... params) {
		// TODO Auto-generated method stub

		AddFriendAccount(mainActivity.recvCalID, mainActivity.recvAccName);
		return null;
	}

	private void AddFriendAccount(String CalID, String AccName) {
		AclRule rule = new AclRule();
		Scope scope = new Scope();
		scope.setType("group");
		scope.setValue(AccName);
		rule.setScope(scope);
		rule.setRole("writer");
		try {
//			if (CalendarLibHelper.client == null) {
//				final HttpTransport transport = AndroidHttp
//						.newCompatibleTransport();
//				final JsonFactory jsonFactory = new GsonFactory();
//				SharedPreferences settings;
//				settings = mainActivity.getPreferences(mainActivity.MODE_PRIVATE);
//				final String PREF_AUTH_TOKEN = "authToken";
//				final String authToken = settings.getString(PREF_AUTH_TOKEN,
//						null);
//
//				HttpRequestInitializer requestInitializer = new HttpRequestInitializer() {
//
//					public void initialize(HttpRequest request)
//							throws IOException {
//						request.getHeaders().setAuthorization(
//								GoogleHeaders.getGoogleLoginValue(authToken));
//					}
//				};
//
//				CalendarLibHelper.client = new com.google.api.services.calendar.Calendar.Builder(
//						transport, jsonFactory, requestInitializer)
//						.setApplicationName(
//						/* "Google-CalendarAndroidSample/1.0" */"Bee To Be")
//						.setJsonHttpRequestInitializer(
//								new GoogleKeyInitializer(ClientCredentials.KEY))
//						.build();
//			}

			CalendarLibHelper.client.acl().insert(CalID, rule).execute();
//			new AsyncInsertEvent(CalID, AccName, FROM_ADDFRIEND).execute();
//			Toast.makeText(mainActivity, "�ʴ�Ϸ�!", 0).show();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			CalendarLibHelper.isFriendAdded = true;
//			CalendarActivity.onRequestCompleted();
		}

	}

}
